import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="test",
    passwd="123456"
)

print(mydb)

mycursor = mydb.cursor()

# CREATE DATABASE
# mycursor.execute("CREATE DATABASE python_db")

# SHOW DATABASES
# mycursor.execute("SHOW DATABASES")
# for x in mycursor:
#     print(x)

mycursor.execute("use python_db")

# mycursor.execute("CREATE TABLE sites(name VARCHAR(255), url VARCHAR(255))")
# mycursor.execute("DROP TABLE sites")
# mycursor.execute("CREATE TABLE sites (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), url VARCHAR(255))")
mycursor.execute("SHOW TABLES")
for x in mycursor:
    print(x)

sql = "INSERT INTO sites(name, url) VALUES (%s, %s)"
val = ("RUNOOB", "https://www.runoob.com")
mycursor.execute(sql, val)

# mycursor.commit()
print(mycursor.rowcount, "记录插入成功, ID:", mycursor.lastrowid)

sql1 = "INSERT INTO sites(name, url) VALUES (%s, %s)"
val1 = [
  ('Google', 'https://www.google.com'),
  ('Github', 'https://www.github.com'),
  ('Taobao', 'https://www.taobao.com'),
  ('stackoverflow', 'https://www.stackoverflow.com/')
]
mycursor.executemany(sql1, val1)

# 提交
mydb.commit()

print(mycursor.rowcount, "记录插入成功。")

mycursor.execute("SELECT * FROM sites")
myresult = mycursor.fetchall()

for x in myresult:
    print(x)